package com.swg.seckill.utils.vo;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.swg.seckill.utils.ValidatorUtil;
import com.swg.seckill.utils.validator.IsMobile;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author swg
 */
public class IsMobileValidator implements ConstraintValidator<IsMobile, Long> {
    private boolean required = false;

    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(Long value, ConstraintValidatorContext context) {
        //必填
        if(required){
            return ValidatorUtil.isMobile(value);
        }else { //非必填
            if(StringUtils.checkValNotNull(value)){
                return true;
            }else {
                return ValidatorUtil.isMobile(value);
            }
        }

    }



}
